home *** CD-ROM | disk | FTP | other *** search
- /*
- WASTE Demo Project:
- Macintosh Controls with Long Values
-
- Copyright © 1993-1995 Marco Piovanelli
- All Rights Reserved
-
- C port by John C. Daub
- */
-
- #ifndef __CONTROLS__
- #include <Controls.h>
- #endif
- #ifndef __FIXMATH__
- #include <FixMath.h>
- #endif
- #ifndef __TOOLUTILS__
- #include <ToolUtils.h>
- #endif
-
- #ifndef __WEDEMOAPP__
- #include "WEDemoIntf.h"
- #endif
-
-
- // long control auxiliary record used for keeping long settings
- // a handle to this record is stored in the reference field of the control record
-
- struct LCAuxRec
- {
- long value; // long value
- long min; // long min
- long max; // long max
- };
- typedef struct LCAuxRec LCAuxRec, *LCAuxPtr, **LCAuxHandle;
-
-
-
- OSErr LCAttach( ControlRef control )
- {
- Handle aux;
- LCAuxPtr pAux;
- OSErr reply = noErr;
-
- /* allocate the auxiliary record that will hold long settings */
-
- aux = NewHandleClear( sizeof( LCAuxRec ) );
- if ( aux == NULL )
- {
- reply = MemError();
- return reply;
- }
-
- /* store a handle to the auxiliary record in the contrlRfCon field */
-
- SetControlReference( control, (long)aux );
-
- /* copy current control settings into the auxiliary record */
-
- pAux = *((LCAuxHandle)aux);
- pAux->value = GetControlValue( control );
- pAux->min = GetControlMinimum( control );
- pAux->max = GetControlMaximum( control );
-
- return reply;
- }
-
-
-
- void LCDetach( ControlRef control )
- {
- Handle aux;
-
- aux = (Handle)GetControlReference( control );
-
- if ( aux != NULL )
- {
- SetControlReference( control, 0 );
- DisposeHandle( aux );
- }
-
- return;
- }
-
-
-
- void LCSetValue( ControlRef control, long value )
- {
- LCAuxPtr pAux;
- short controlMin, controlMax, newControlValue;
-
- pAux = *((LCAuxHandle)GetControlReference( control ));
-
- /* make sure value is in the range min...max */
-
- if ( value < pAux->min )
- value = pAux->min;
- if ( value > pAux->max )
- value = pAux->max;
-
- /* save value in auxiliary record */
-
- pAux->value = value;
-
- /* calculate new thumb position */
-
- controlMin = GetControlMinimum( control );
- controlMax = GetControlMaximum( control );
- newControlValue = controlMin + FixRound( FixMul ( FixDiv( value - pAux->min,
- pAux->max - pAux->min), BSL(controlMax - controlMin, 16 )));
-
- /* do nothing if the thumb position hasn't changed */
-
- if ( newControlValue != GetControlValue(control) )
- SetControlValue( control, newControlValue );
-
- return;
- }
-
- void LCSetMin( ControlRef control, long min )
- {
- LCAuxPtr pAux;
-
- pAux = *((LCAuxHandle)GetControlReference( control ));
-
- /* make sure min is less than or equal to max */
-
- if ( min > pAux->max )
- min = pAux->max;
-
- /* save min in auxiliary record */
- pAux->min = min;
-
- /* set contrlMin field to min or SHRT_MIN, whichever is greater */
-
- if ( min < SHRT_MIN )
- min = SHRT_MIN;
-
- SetControlMinimum( control, min );
-
- /* reset value */
-
- LCSetValue( control, pAux->value );
-
- return;
- }
-
- void LCSetMax( ControlRef control, long max )
- {
- LCAuxPtr pAux;
-
- pAux = *((LCAuxHandle)GetControlReference( control ));
-
- /* make sure max is greater than or equal to min */
-
- if ( max < pAux->min )
- max = pAux->min;
-
- /* save max in auxiliary record */
-
- pAux->max = max;
-
- /* set contrlMax field to max or SHRT_MAX, whichever is less */
-
- if ( max > SHRT_MAX )
- max = SHRT_MAX;
-
- SetControlMaximum( control, max );
-
- /* reset value */
-
- LCSetValue( control, pAux->value );
-
- return;
- }
-
- /* In each of these LCGetXXX() functions, there are 2 ways listed to do things. They are
- both the same thing and perform the same stuff, just one is easier to read than the
- other (IMHO). I asked Marco about it and he gave me the shorter code (what's commented
- in each function) and gave me this explanation:
-
- This version [the commented code] yields smaller and faster code
- (try disassembling both versions if you wish), but some people may
- find it somewhat harder to read.
-
- I agree with Marco that his code is better overall, but in the interest of readabilty
- (since this demo is a learning tool), I left my code in and put Marco's in commented
- out. Pick whichever you'd like to use.
- */
-
-
- long LCGetValue( ControlRef control )
- {
- LCAuxPtr pAux;
-
- pAux = *((LCAuxHandle)GetControlReference( control ));
-
- return pAux->value;
-
- // this is Marco's code. Remember, this is a little harder to read, but overall
- // yields tighter code.
-
- // return (* (LCAuxHandle) GetControlReference(control)) -> value;
-
- }
-
-
- long LCGetMin( ControlRef control )
- {
- LCAuxPtr pAux;
-
- pAux = *((LCAuxHandle)GetControlReference( control ));
-
- return pAux->min;
-
- // this is Marco's code. Remember, this is a little harder to read, but overall
- // yields tighter code.
-
- // return (* (LCAuxHandle)GetControlReference(control)) -> min;
-
- }
-
-
- long LCGetMax( ControlRef control )
- {
- LCAuxPtr pAux;
-
- pAux = *((LCAuxHandle)GetControlReference( control ));
-
- return pAux->max;
-
- // this is Marco's code. Remember, this is a little harder to read, but overall
- // yields tighter code.
-
- // return (* (LCAuxHandle)GetControlReference(control)) -> max;
-
- }
-
-
- void LCSynch( ControlRef control )
- {
- LCAuxPtr pAux;
- short controlMin, controlMax, controlValue;
-
- controlMin = GetControlMinimum( control );
- controlMax = GetControlMaximum( control );
- controlValue = GetControlValue( control );
- pAux = *((LCAuxHandle)GetControlReference( control ));
-
- /* calculate new long value */
-
- pAux->value = pAux->min + FixMul( FixRatio ( controlValue - controlMin,
- controlMax - controlMin), pAux->max - pAux->min );
-
- return;
- }
-
-